package com.zlll.winner.service.sysUser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zlll.winner.business.model.sysUser.SysMenu;
import com.zlll.winner.business.model.sysUser.SysUser;
import com.zlll.winner.business.vo.sysUser.MenuItemVo;
import com.zlll.winner.mapper.sysUser.SysMenuMapper;
import com.zlll.winner.business.service.sysUser.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlll.winner.mapper.sysUser.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.dubbo.config.annotation.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author ricky
 * @since 2019-09-06
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 构建用户菜单树
     * @param userId
     * @param project
     * @return
     */
    @Override
    public List<MenuItemVo> getUserMenuTree(Long userId, String project, boolean needButton) {
        List<SysMenu> menus = this.getUserMenuList(userId,project,needButton);
        List<MenuItemVo> menuList = new ArrayList<>();
        return createMenuTree(menus, menuList);
    }

    /**
     * 获取用户的菜单信息
     * @param userId 用户id
     * @param project 项目code
     * @return
     */
    @Override
    public List<SysMenu> getUserMenuList(Long userId, String project, boolean needButton) {
        List<SysMenu> menus = null;
        SysUser user = sysUserMapper.selectById(userId);
        if (0==user.getUserType().intValue()) {  //超级管理员   查询所有菜单数据
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("project",project);
            if(needButton){
                queryWrapper.in("button",0,1);
            }else{
                queryWrapper.eq("button",0);
            }
            queryWrapper.orderByAsc("sort");
            menus = sysMenuMapper.selectList(queryWrapper);
        } else {
            if(needButton){
                //此时需要菜单和按钮
                menus = sysMenuMapper.selectByRole(userId,project,1);
            }else{
                //此时只要菜单
                menus = sysMenuMapper.selectByRole(userId,project,0);
            }
        }
        return menus;
    }

    /**
     * 菜单树构建方法
     * @param menus
     * @param menuList
     * @return
     */
    private List<MenuItemVo> createMenuTree(List<SysMenu> menus, List<MenuItemVo> menuList) {
        if(menus != null && menus.size() > 0){
            List<SysMenu> finalMenus = menus;
            menus.stream().filter(menu -> menu.getParentId().equals(0L)).forEach(menu -> {
                MenuItemVo menuItem = packingMenuItemVo(menu);
                findChildren(menuItem, finalMenus);
                menuList.add(menuItem);
            });
        }
        return menuList;
    }

    /**
     * 递归查找children
     * @param parent
     * @param allList
     */
    private void findChildren(MenuItemVo parent,List<SysMenu> allList){
        List<SysMenu> menuList = allList.stream().filter(menu -> menu.getParentId().toString().equals(parent.getModuleId())).collect(Collectors.toList());
        if( menuList!= null && menuList.size() > 0){
            List<MenuItemVo> children = new ArrayList<>(menuList.size());
            menuList.forEach(menu -> {
                MenuItemVo menuItemVo = packingMenuItemVo(menu);
                findChildren(menuItemVo,allList);
                children.add(menuItemVo);
            });
            parent.setChildren(children);
            parent.setHasChild(true);
        }
    }

    /**
     * 菜单内容封装
     * @param menu
     * @return
     */
    private MenuItemVo packingMenuItemVo(SysMenu menu){
        MenuItemVo menuItem = new MenuItemVo();
        menuItem.setId(menu.getId().toString());
        menuItem.setModuleId(menu.getId().toString());
        menuItem.setParentId(menu.getParentId().toString());
        menuItem.setCode(menu.getCode());
        menuItem.setImgUrl(menu.getImgUrl());
        menuItem.setName(menu.getName());
        menuItem.setSort(menu.getSort());
        menuItem.setType(menu.getType());
        menuItem.setUrl(menu.getUrl());
        return menuItem;
    }
}
